<?php
/**
 *  file: doitphp Indexcontrller.class.php
 *  
 *  权限控制管理 范例
 *  @author tommy <streen003@gmail.com>
 *  @version 1.0 2010-12-20
 */

class IndexController extends Controller {
	
	public function indexAction() {
		
		//parse login status
		$login_states = $this->is_login();
		if(!$login_states) {
			$this->redirect($this->create_url('index/login'));
		}
		
		//get params
		$mode = $this->get('mode');
		$page = $this->get('page');
		$mode = empty($mode) ? 1 : $mode;
		$page = empty($page) ? 1 : (int)$page;
		
		//instance model
		$option_model 	= $this->model('acl_options');
		$role_model		= $this->model('acl_roles');
		
		$page_list_num = 10;
		
		//parase kinds of mode
		switch ($mode) {
			case 1:								
				$total_num = $option_model->from('acl_options', array('total_num'=>'count(`auth_option_id`)'))->select(false);
				$total_num = $total_num['total_num'];				
				
				$list_data = $option_model->from('acl_options')->page_limit($page, $page_list_num)->select();		
				break;
				
			case 2:
				$total_num = $role_model->from('acl_roles', array('total_num'=>'count(`role_id`)'))->select(false);
				$total_num = $total_num['total_num'];
				
				$list_data = $role_model->from('acl_roles')->page_limit($page, $page_list_num)->select();
				break;
				
			case 3:
				$total_num = $role_model->from('member', array('total_num'=>'count(`user_id`)'))->select(false);
				$total_num = $total_num['total_num'];
				
				$handle_data 	= $role_model->from('member')->page_limit($page, $page_list_num)->select();
				$list_data 		= array();
				foreach ($handle_data as $key=>$lines) {
					$list_data[$key]['id'] 		= $lines['user_id'];
					$list_data[$key]['name'] 	= $lines['name'];
					
					$role_name = $role_model->from('acl_roles', 'role_name')->where('role_id=' . $lines['role_id'])->select(false);
					$list_data[$key]['role']	= $role_name['role_name'];
				}
				
				$role_data = $role_model->from('acl_roles', array('role_id', 'role_name'))->select();
				$select_box_array = array();
				foreach ($role_data as $lines) {
					$select_box_array[$lines['role_id']] = $lines['role_name'];
				}
				unset($role_data);
				$this->assign('select_array', $select_box_array);
				break;		
		}
		
		//handle pagelist		
		$pagelist = $this->instance('pagelist');
		
		//assign param
		$this->assign(array(
		'mode'			=> $mode,
		'page'			=> $page,
		'list_data'		=> $list_data,
		'pagelist_css'	=> $pagelist->load_css(),
		'pagelist_html'=> $pagelist->total($total_num)->url($this->create_url('index/index', array('model'=>$mode, 'page'=>null), false))->num($page_list_num)->page($page)->output(),
		));
		
		//display page
		$this->display();
	}
	
	/**
	 * 用户设置页面
	 */
	public function config_userAction() {
		//parse login status
		$login_states = $this->is_login();
		if(!$login_states) {
			$this->redirect($this->create_url('index/login'));
		}
		
		//get params
		$user_id 	= (int)$this->get('id');
		if (!$user_id) {
			exit();
		}
		
		//instance model
		$member_model = $this->model('member');
		
		//get user data
		$user_data 		= $member_model->from(array('m'=>'member', 'r'=>'acl_roles'), array('m.user_id', 'm.name', 'r.role_name'))->where('m.user_id=' . $user_id)->where('m.role_id=r.role_id')->select(false);		
		$acl_user_data 	= $member_model->from(array('au'=>'acl_users', 'ao'=>'acl_options'), array('au.auth_setting', 'au.auth_option_id', 'ao.auth_option_name'))->where('au.user_id=' . $user_id)->where('au.auth_option_id=ao.auth_option_id')->select();		
		
		//assign params
		$this->assign(array(
		'data'			=> $user_data,
		'list_data'		=> $acl_user_data,
		));
		
		//display page
		$this->display();
	}
	
	/**
	 * 用户设置(user option_id setting)
	 */
	public function ajax_user_settingAction() {
		//parase login state
		$login_states = $this->is_login();
		if(!$login_states) {
			exit();
		}
		
		//get params
		$user_id 		= (int)$this->post('user_id');
		$acl_option_id 	= (int)$this->post('acl_option_id');
		$setting		= (int)$this->post('setting');
		if (!$user_id || !$acl_option_id) {
			exit();
		}

		//instance model
		$member_model = $this->model('member');
		
		echo ($member_model->query("UPDATE `acl_users` SET `auth_setting` = '{$setting}' WHERE `user_id` ={$user_id} AND `auth_option_id` ={$acl_option_id}")) ? 101 : 100;
	}
	
	/**
	 * ajax处理用户编辑
	 */
	public function ajax_handle_edit_userAction() {
		//parase login state
		$login_states = $this->is_login();
		if(!$login_states) {
			exit();
		}
		
		//get params
		$user_id 	= (int)$this->post('user_id');
		$user_name 	= $this->post('user_name');
		$role_id 	= $this->post('role_id');
		if (!$user_id || !$user_name) {
			exit();
		}
		
		//instance model
		$member_model = $this->model('member');
		
		$user_data = $member_model->find($user_id);
		
		$update_array = array();
		if ($user_data['name'] != $user_name) {
			$check_data = $member_model->getOne('name=?', $user_name);
			if (!$check_data) {
				$update_array['name'] = $user_name;
			} else {
				echo 103;
				exit();
			}
		}
		
		if ($user_data['role_id'] != $role_id) {
			$update_array['role_id'] = $role_id;
			
			//update acl user data
			$member_model->query("DELETE FROM `acl_users` WHERE `user_id` = {$user_id}");
			$role_data = $member_model->execute("SELECT * FROM `acl_roles_data` WHERE `role_id` ={$role_id}");
			
			foreach ($role_data as $lines) {
				$member_model->query("INSERT INTO `acl_users`(`user_id` ,`auth_option_id` ,`auth_setting`) VALUES ('{$user_id}', '{$lines['acl_option_id']}', '1')");
			}
		}
		
		if ($update_array) {
			echo ($member_model->update($update_array, 'user_id=' . $user_id)) ? 101 : 100;
		} else {
			echo 101;
		}		
	}
	
	/**
	 * 编辑用户页面
	 */
	public function ajax_edit_userAction() {
		//parase login state
		$login_states = $this->is_login();
		if(!$login_states) {
			exit();
		}
		
		//get params
		$user_id = (int)$this->get('id');
		if (!$user_id) {
			exit();
		}
		
		//instance model
		$member_model = $this->model('member');
		
		$data = $member_model->find($user_id);
		
		//get role data
		$role_data = $member_model->from('acl_roles', array('role_id', 'role_name'))->select();
		$select_box_array = array();
		foreach ($role_data as $lines) {
			$select_box_array[$lines['role_id']] = $lines['role_name'];
		}
		unset($role_data);
		
		//assign params
		$this->assign(array(
		'data'				=> $data,
		'role_select_box' => html::select($select_box_array, array('name'=>'role_id', 'id'=>'role_id_box'), $data['role_id']),
		));
		
		//display page
		$this->display();
	}
	
	/**
	 * ajax添加用户
	 */
	public function ajax_insert_userAction() {
		//parase login state
		$login_states = $this->is_login();
		if(!$login_states) {
			exit();
		}
		
		//get params
		$user_name 	= $this->post('user_name');
		$role_id	= (int)$this->post('role_id');
		if (!$user_name || !$role_id) {
			exit();
		}
		
		//instance model
		$member_model 	= $this->model('member');		
		
		//insert data
		$member_model->insert(array('name'=>$user_name, 'role_id'=>$role_id));
		$user_id = $member_model->get_insert_id();
		
		//insert acl users
		$role_data = $member_model->execute("SELECT * FROM `acl_roles_data` WHERE `role_id` ={$role_id}");
		foreach ($role_data as $lines) {
			$member_model->query("INSERT INTO `acl_users` (`user_id` ,`auth_option_id` ,`auth_setting`) VALUES ('{$user_id}', '{$lines['acl_option_id']}', '1')");
		}
		
		echo 101;
	}
	
	/**
	 * ajax删除user
	 */
	public function ajax_delete_userAction() {
		//parase login state
		$login_states = $this->is_login();
		if(!$login_states) {
			exit();
		}
		
		//get params
		$user_id = (int)$this->post('user_id');
		if (!$user_id) {
			exit();
		}
		
		//instance model
		$member_model = $this->model('member');
		
		$member_model->delete('user_id=' . $user_id);
		
		echo ($member_model->query("DELETE FROM `acl_users` WHERE `user_id` = {$user_id}")) ? 101 : 100; 
	}
	
	/**
	 * ajax编辑role
	 */
	public function ajax_handle_edit_roleAction() {
		//parase login state
		$login_states = $this->is_login();
		if(!$login_states) {
			exit();
		}
		
		//get params
		$role_id			= (int)$this->post('id');
		$role_name			= $this->post('role_name');
		$role_description	= $this->post('role_description');		
		if (!$role_id || !$role_name) {
			exit();
		}
		
		//instance model
		$role_model = $this->model('acl_roles');
		
		$data = $role_model->find($role_id);
		if ($data['role_name'] == $role_name) {
			echo 101;
			exit();
		} else {
			if ($role_model->getOne('role_name=?', $role_name)) {
				echo 103;
				exit();
			}
		}

		echo ($role_model->update(array('role_name'=>$role_name, 'role_description'=>$role_description), 'role_id=' . $role_id)) ? 101 : 100;
	}
	
	/**
	 * 设置role的option
	 * 
	 * @access public
	 */
	public function config_roleAction() {		
		//parse login status
		$login_states = $this->is_login();
		if(!$login_states) {
			$this->redirect($this->create_url('index/login'));
		}
		
		//get params
		$role_id = (int)$this->get('id');
		if (!$role_id) {
			exit();
		}
		
		//instance model
		$role_model = $this->model('acl_roles');
		$option_model 	= $this->model('acl_options');
		
		$role_data = $role_model->find($role_id);
		
		$list_data = $option_model->findAll();
				
		$selected_data = $role_model->from('acl_roles_data')->where('role_id=' . $role_id)->select();
		$selected_array = array();
		foreach ($selected_data as $lines) {
			$selected_array[] = $lines['acl_option_id'];
		}
		
		//assign params
		$this->assign(array(
		'role_id'			=> $role_id,
		'role_data'			=> $role_data,
		'list_data'			=> $list_data,
		'selected_array'	=> $selected_array,
		));
		
		//display page
		$this->display();
	}
	
	/**
	 * ajax完成role设置
	 */
	public function ajax_handle_config_roleAction() {
		//parase login state
		$login_states = $this->is_login();
		if(!$login_states) {
			exit();
		}
		
		//get params
		$list_id = $this->post('list_id');
		$role_id = (int)$this->post('role_id');
		if (!$role_id) {
			exit();
		}
		
		if (!$list_id) {
			echo '请选择所要设置的option';
			exit();
		}		
		$list_array = explode(',', $list_id);
		
		//instance model
		$role_model = $this->model('acl_roles');
		
		$select_data = $role_model->from('acl_roles_data')->where('role_id=' . $role_id)->select();
		$select_array = array();
		foreach ($select_data as $lines) {
			$select_array[] = $lines['acl_option_id'];
		}
		
		$insert_array = array_diff($list_array, $select_array);
		$delete_array = array_diff($select_array, $list_array);
		
		//insert data
		if ($insert_array) {
			foreach ($insert_array as $value) {
				$role_model->query("INSERT INTO `acl_roles_data` (`role_id` ,`acl_option_id`)VALUES ('{$role_id}', '{$value}')");
			}
		}
		
		//delete data
		if ($delete_array) {
			$delete_id_list = implode(',', $delete_array);
			$role_model->query("DELETE FROM `acl_roles_data` WHERE `acl_option_id` IN({$delete_id_list})");
		}
		
		echo 101;
	}
	
	/**
	 * 编辑role
	 */
	public function ajax_edit_acl_roleAction() {
		//parase login state
		$login_states = $this->is_login();
		if(!$login_states) {
			exit();
		}
		
		//get params
		$role_id = (int)$this->get('id');
		if (!$role_id) {
			exit();
		}
		
		//instance model
		$role_model = $this->model('acl_roles');
		
		//get data
		$data = $role_model->find($role_id);
		
		include $this->template();
	}
	
	/**
	 * 删除acl role
	 */
	public function ajax_delete_acl_roleAction() {
		//parase login state
		$login_states = $this->is_login();
		if(!$login_states) {
			exit();
		}
		
		//get params
		$role_id = (int)$this->post('id');
		if (!$role_id) {
			exit();
		}
		
		//instance model
		$role_model = $this->model('acl_roles');
		
		//delete role option data
		$role_model->query("DELETE FROM `acl_roles_data` WHERE `role_id` = {$role_id}");
		
		echo ($role_model->delete('role_id=' . $role_id)) ? 101 : 100;
	}
	
	/**
	 * ajax添加新角色
	 * 
	 */
	public function ajax_insert_acl_roleAction() {
		//parase login state
		$login_states = $this->is_login();
		if(!$login_states) {
			exit();
		}
		
		//get params
		$role_name 			= $this->post('role_name');
		$role_description 	= $this->post('description');
		if (!$role_name) {
			exit();
		}
		
		//instance model
		$role_model = $this->model('acl_roles');
		
		//parse same data
		$check_data = $role_model->getOne('role_name=?', $role_name);
		if ($check_data) {
			echo 103;
			exit();
		}
		echo ($role_model->insert(array('role_name'=>$role_name, 'role_description'=>$role_description))) ? 101 : 100;
	}
	
	/**
	 * 删除acl option
	 */
	public function ajax_delete_acl_optionAction() {
		//parase login state
		$login_states = $this->is_login();
		if(!$login_states) {
			exit();
		}
		
		//get params
		$option_id = (int)$this->post('auth_option_id');
		if (!$option_id) {
		 	exit();
		}
		
		//instance model
		$option_model 	= $this->model('acl_options');
		
		//delete role option data
		$option_model->query("DELETE FROM `acl_roles_data` WHERE `acl_option_id` = {$option_id}");
		$option_model->query("DELETE FROM `acl_users` WHERE `auth_option_id` = {$option_id}");
		
		echo ($option_model->delete('auth_option_id=' . $option_id)) ? 101 : 100;
	}
	
	/**
	 * 编辑acl option
	 */
	public function ajax_edit_acl_optionAction() {
		//parase login state
		$login_states = $this->is_login();
		if(!$login_states) {
			exit();
		}
		
		//get params
		$auth_option_id = (int)$this->get('id');
		if (!$auth_option_id) {
			exit();
		}
		
		//instance model
		$option_model 	= $this->model('acl_options');
		
		$data = $option_model->find($auth_option_id);
				
		print <<<EOT
<table width="420" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td height="30" colspan="2" align="center">&nbsp;</td>
  </tr>
  <tr>
    <td width="120" height="30" align="center">Acl Option：</td>
    <td width="300" align="left"><input type="text" name="acl_option_box" id="acl_option_box" value="{$data['auth_option_name']}"/></td>
  </tr>
  <tr>
    <td height="15" colspan="2" align="center"></td>
  </tr>
  <tr>
    <td height="30" colspan="2" align="center"><input type="button" name="edit_acl_option_button" value="编辑" onclick="edit_acl_option({$data['auth_option_id']})"/></td>
  </tr>
</table>
EOT;
	}
	
	/**
	 * 编辑 acl option
	 */
	public function ajax_handle_edit_acl_optionAction() {		
		//parase login state
		$login_states = $this->is_login();
		if(!$login_states) {
			exit();
		}
		
		//get params
		$auth_option_id 	= (int)$this->post('id');
		$option_name    	= $this->post('option_name');
		if (!$auth_option_id || !$option_name) {
			exit();
		}
		
		//instance model
		$option_model 	= $this->model('acl_options');
		
		$check_data = $option_model->getOne('auth_option_name=?', $option_name);
		if ($check_data) {
			echo 103;
			exit();
		}
		echo ($option_model->update(array('auth_option_name'=>$option_name), 'auth_option_id='.$auth_option_id)) ? 101 : 100;
	}
	
	/**
	 * 添加acl option	  
	 */
	public function ajax_insert_acl_optionAction() {
		//parase login state
		$login_states = $this->is_login();
		if(!$login_states) {
			exit();
		}
		
		//get params
		$option_name = $this->post('option');		
		if (!$option_name) {
			exit();
		}
		
		//instance model
		$option_model 	= $this->model('acl_options');
		
		//parse acl option table wehter have same optin name
		$check_data = $option_model->getOne('auth_option_name=?', $option_name);
		if ($check_data) {
			echo '103';
			exit();
		}
		
		echo ($option_model->insert(array('auth_option_name'=>$option_name))) ? 101 : 100;		
	}
	
	public function loginAction() {
		
		//parase login state
		if ($this->is_login()) {
			$this->redirect($this->create_url('index/index'));
		}
		
		$this->display();
	}
	
	/**
	 * 检测管理员是否登陆
	 * 
	 * @return boolean
	 */
	public function is_login() {
		
		return (cookie::get('doit_acl_login') == true) ? true : false;
	}
	
	/**
	 * 处理登陆
	 * 
	 * @return void
	 */
	public function handle_loginAction() {
		
		//get params
		$name 		= $this->post('user_name');
		$password 	= $this->post('password');
		
		//show error message
		if (!$name || !$password) {
			$this->show_message('用户名或密码不能为空！', -1, 3);
		}
		
		if ($name == 'doit' && $password = '123456') {
			cookie::set('doit_acl_login', true, 3600*6);
			$this->redirect($this->create_url('index/index'));
		} else {
			$this->show_message('用户名或密码错误！请重新提交', -1, 3);
		}
	}
}